clc;
clear all;
close all;

VecLength = @( vec_ ) sqrt( vec_(1)^2 + vec_(2)^2 + vec_(3)^2 );
VecUnit = @( vec_ )	vec_ ./ VecLength( vec_ );
TriMid = @( v1_, v2_, v3_ ) ( v1_ + v2_ + v3_ ) ./ 3;
TetMid = @( v1_, v2_, v3_, v4_ ) ( v1_ + v2_ + v3_ + v4_ ) ./ 4;

vx = [ 1; 0; 0 ];
vy = [ 0; 1; 0 ];
vz = [ 0; 0; 1 ];
vn = [ -1/3; -1/3; -1/3 ];

vm_xyz = TriMid( vx, vy, vz );
vm_xyn = TriMid( vx, vy, vn );
vm_xzn = TriMid( vx, vz, vn );
vm_yzn = TriMid( vy, vz, vn );

vm_xyzn = TetMid( vx, vy, vz, vn );

v1 = vm_xyzn + 5 .* ( vm_xyz - vm_xyzn );
v2 = vm_xyzn + 5 .* ( vm_xyn - vm_xyzn );
v3 = vm_xyzn + 5 .* ( vm_xzn - vm_xyzn );
v4 = vm_xyzn + 5 .* ( vm_yzn - vm_xyzn );

vertexCount = 8;
vertexList = [ vx, vy, vz, vn, v1, v2, v3, v4 ];

tetraCount = 5;
tetraVertexIndex = [ ...
	[1;2;3;4], ...
	[1;2;3;5], ...
	[1;2;4;6], ...
	[1;3;4;7], ...
	[2;3;4;8] ...
];

faceCount = 16;
faceVertexIndex = [ ...
	[1;2;3], ...
	[1;2;4], ...
	[1;3;4], ...
	[2;3;4], ...
	[1;2;5], ...
	[1;3;5], ...
	[2;3;5], ...
	[1;2;6], ...
	[1;4;6], ...
	[2;4;6], ...
	[1;3;7], ...
	[1;4;7], ...
	[3;4;7], ...
	[2;3;8], ...
	[2;4;8], ...
	[3;4;8], ...
];

tetraFaceIndex = [ ...
	[1;2;3;4], ...
	[1;5;6;7], ...
	[2;8;9;10], ...
	[3;11;12;13], ...
	[4;14;15;16] ...
];

faceTetraIndex = [ ...
	[1;2], ...
	[1;3], ...
	[1;4], ...
	[1;5], ...
	[2;-1], ...
	[2;-1], ...
	[2;-1], ...
	[3;-1], ...
	[3;-1], ...
	[3;-1], ...
	[4;-1], ...
	[4;-1], ...
	[4;-1], ...
	[5;-1], ...
	[5;-1], ...
	[5;-1], ...
];

%%% DRAW

figure();
hold on;

patch( 'Faces', faceVertexIndex( :, tetraFaceIndex( :, 1 )' )', ...
	'Vertices', vertexList', ...
	'FaceColor', [ 0, 0, 0 ], ...
	'FaceAlpha', 0.3 ...
);
patch( 'Faces', faceVertexIndex( :, tetraFaceIndex( :, 2 )' )', ...
	'Vertices', vertexList', ...
	'FaceColor', [ 1, 1, 0 ], ...
	'FaceAlpha', 0.3 ...
);
patch( 'Faces', faceVertexIndex( :, tetraFaceIndex( :, 3 )' )', ...
	'Vertices', vertexList', ...
	'FaceColor', [ 1, 0, 0 ], ...
	'FaceAlpha', 0.3 ...
);
patch( 'Faces', faceVertexIndex( :, tetraFaceIndex( :, 4 )' )', ...
	'Vertices', vertexList', ...
	'FaceColor', [ 0, 1, 0 ], ...
	'FaceAlpha', 0.3 ...
);
patch( 'Faces', faceVertexIndex( :, tetraFaceIndex( :, 5 )' )', ...
	'Vertices', vertexList', ...
	'FaceColor', [ 0, 0, 1 ], ...
	'FaceAlpha', 0.3 ...
);

plot3( [ -2, 2 ], [ 0, 0 ], [ 0, 0 ], 'r', 'LineWidth', 2 );
plot3( [ 0, 0 ], [ -2, 2 ], [ 0, 0 ], 'g', 'LineWidth', 2 );
plot3( [ 0, 0 ], [ 0, 0 ], [ -2, 2 ], 'b', 'LineWidth', 2 );

hold off;

axis square;
axis equal;
axis vis3d;
axis off;

camproj('perspective');




